package com.beyondsoft.users.receiver;

import com.beyondsoft.common.core.constant.TransactionConstants;
import com.beyondsoft.common.core.exchange.model.Message;
import com.beyondsoft.common.core.sys.initialization.RabbitConfig;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 接收主线程的处理通知
 */
@Component
@RabbitListener(queues = RabbitConfig.FANOUT_USERS_QUEUE)
public class SubReceiver {

    @RabbitHandler
    public void process(Message message) {
        //TODO 不接收离线消息
        System.out.println("USERS收到处理通知");
        Long lock = TransactionConstants.getTransactionLock(message.getPackageKey());
        synchronized (lock) {
            //更新状态
            TransactionConstants.setTransactionState(message.getPackageKey(), message.getState());
            //释放锁
            lock.notify();
        }
    }

}
